/** utility will be loaded to client **/
{if(window.zktc){return;}

var tripId = (jq.now() % 9999) + 1;

var args2array = function(args,start){
	var arr = [];
	var s = start?start:0;
	var e = args.length;
	for(;s<e;s++){
		arr.push(args[s]);
	}
	return arr;
};


var zktc= {};
window.zktc = zktc;

zktc._onResponse = function(){
	tripId++;
	if(tripId>9999){
		tripId = 0;
	}
};

zWatch.listen({
	onResponse : [zktc,zktc._onResponse]
})

//return safe value that could return by selenium
zktc.toSafeValue = function(value){
	var type = typeof value;
	if(type=="number" || type=="string" || type=="boolean"){
		return value;
	}
	return null;
}

zktc._toGadgetId = function(w){
	return w.uuid;
}
zktc._toWidget = function(gid){
	return zk.Widget.$(gid);
}

zktc.getTripId = function(){
	return tripId;
}

zktc.findElement = function(selector){
	var r = jq(selector);
	r = r.size()>0?r.get(0):null;
	return r;
};

zktc.findElements = function(selector){
	var r = [];
	jq(selector).each(function(){
		r.push(this);
	});
	return r;
};


zktc.getChildrenElement = function(element){
	var r = [];
	jq(element).children().each(function(){
		r.push(this);
	});
	return r;
};

/** note, return the widgetid */
zktc.findWidget = function(selector,strict){
	var r;
	jq(selector).each(function(){
		var w = zk.Widget.$(this,{strict:strict?strict:false}); 
		if(w){
			r = zktc._toGadgetId(w);
			return false;
		}
	});
	return r;
};

/** note return the widgetids*/
zktc.findWidgets = function(selector,strict){
	var r = [];
	jq(selector).each(function(){
		var w = zk.Widget.$(this,{strict:strict?strict:false}); 
		if(w){
			var n = zktc._toGadgetId(w);
			if(n){
				r.push(n);
			}
		}
	});
	return r;
};

var util = {};
zktc.util = util;
util.toGet = function(name){
	var get = "get"+name.charAt(0).toUpperCase()+name.substring(1,name.length);
	return get;
}
util.toIs = function(name){
	var get = "is"+name.charAt(0).toUpperCase()+name.substring(1,name.length);
	return get;
}
util.toSet = function(name){
	var get = "set"+name.charAt(0).toUpperCase()+name.substring(1,name.length);
	return get;
}


var wgts = {};
zktc.wgts = wgts;

wgts.getId = function(gid){
	var w = zktc._toWidget(gid);
	return w?w.id:null;
};

wgts.getDom = function(gid){
	var w = zktc._toWidget(gid);
	if(w){
		return w.$n();
	}
}

wgts.getRealElement = function(gid){
	var w = zktc._toWidget(gid);
	var e;
	if(w){
		e = w.$n('real');
		if(!e) e = w.$n();
	}
	return e;
};

wgts.getParent = function(gid,height){
	var w = zktc._toWidget(gid);
	if(w){
		w = w.parent;
	}
	while(w && height > 0){
		w = w.parent;
		height--;
	}
	return w?zktc._toGadgetId(w):null;
};

wgts.getChildren = function(gid){
	var w = zktc._toWidget(gid);
	var r = [];
	if(w){
		var c = w.firstChild;
		while(c){
			var n = zktc._toGadgetId(c);
			r.push(n);
			c = c.nextSibling;
		}
	}
	return r;
};

wgts.getAttribute = function(gid,name){
	var w = zktc._toWidget(gid);
	if(w){
		var fn = util.toGet(name);
		fn = w[fn];
		if(typeof fn == "function"){
			var r = fn.apply(w);
			return zktc.toSafeValue(r);
		}
		
		fn = util.toIs(name);
		fn = w[fn];
		if(typeof fn == "function"){
			var r = fn.apply(w);
			return zktc.toSafeValue(r);
		}
		
		if(w[name]){
			return zktc.toSafeValue(w[name]);
		}
	}
	return null;
};

wgts.setAttribute = function(gid,name,val){
	var w = zktc._toWidget(gid);
	if(w){
		var fn = util.toSet(name);
		fn = w[fn];
		if(typeof fn == "function"){
			fn.apply(w,[val]);
		}else if(typeof w[name] != "undefined"){
			w[name] = val;
		}
	}
};

wgts.callFunction = function(gid,name){
	var w = zktc._toWidget(gid);
	if(w){
		var fn = w[name];
		if(typeof fn == "function"){
			var args = args2array(arguments,2);
			var r = fn.apply(w,args);
			return zktc.toSafeValue(r);
		}
	}
	return null;
};

wgts.dump = function(gid,dom){
	zk.load('zk.debug',function(){
		var w = zktc._toWidget(gid);
		if(w){
			if(dom){
				zDebug.dumpDomTree(w);
			}else{
				zDebug.dumpWidgetTree4Zul(w);
			}
		}
	});
}

wgts.getWidgetAttribute = function(gid,name){
	var w = zktc._toWidget(gid);
	var c;
	if(w){
		var fn = util.toGet(name);
		fn = w[fn];
		if(typeof fn == "function"){
			c = fn.apply(w);
		}else if(w[name]){
			c = w[name];
		}
	}
	if(c){
		return zktc._toGadgetId(c);
	}
};

wgts.doHide = function(gid){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.hide();
	return sid;
};
wgts.doShow = function(gid){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.show();
	return sid;
};
wgts.doClick = function(gid){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.doClick_(new zk.Event(w, 'onClick',{}))
	return sid;
};
wgts.doDoubleClick = function(gid){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.doDoubleClick_(new zk.Event(w, 'onDoubleClick',{}))
	return sid;
};
wgts.doRightClick = function(gid){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.doRightClick_(new zk.Event(w, 'onRightClick',{}))
	return sid;
};
wgts.doFocus = function(gid,timeout){
	var w = zktc._toWidget(gid);
	var sid = tripId;
	if(w) w.focus(timeout);
	return sid;
};

}
